<?php

namespace App\Http\Controllers;

use App\Record;
use Illuminate\Http\Request;
use App\loan_customer;

class FinanceController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $list=loan_customer::where('status', 1)->paginate(12);
        return view('finance.index',compact('list'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
//        $list=loan_customer::where('id',$id)->first();

        $lists = Record::where('pid',$id)->with('loans')->paginate(12);
//        dd($lists);

        return view('finance.record',compact('lists'));

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
    public  function state($type, $id){
        $info = loan_customer::find($id);

        if($type=='no'){
            $info->update(['state' => 1]);

            $info->record()->create([
                'moneys' => 0,
                'frequency' => $info->record()->latest()->value('frequency') ?: 0,
            ]);

            return ['fail' => '不正常'];
        } else {

            $info->update(['state' => 0]);
            $info->record()->create([
                'moneys' => $info->repayment,
                'frequency' => $info->record()->latest()->value('frequency') + 1 ?: 1,
            ]);


            if($info->record()->latest()->value('frequency') == $info->limits){
                $info->update(['state' => 2]);
            }
            return ['success' => '正常'];
        }
    }
}
